<?php

namespace app\admin\controller;

use app\common\model\Member;
use app\common\model\MemberPackages;
use app\common\model\Packages;
use app\common\model\UcenterMember;
use think\Db;
use app\common\model\Traders as tradersModel;
use app\common\api\controller\Bonus;
use app\common\api\controller\User as UserApi;
use app\common\api\controller\Error;
use think\Validate;


class Traders extends Home
{

    public function lists()
    {
        $where = $this->getMap('update_time');
        $map = $where['map'];
        $maps = $where['maps'];

        $list = tradersModel::where('status', 1)->order('id desc')->paginate(20, false, ['query' => $maps]);

        $page = $list->render();
        foreach ($list as $v) {
            $v->usernumber = Member::where('uid', $v->uid)->value('usernumber');
        }
        $this->assign('_list', $list);
        $this->assign('page', $page);

        /*追加查询条件*/
        $arr = array();
        $this->searchCondition($maps, $arr);

        $this->assign('email', self::$adminInfo['email']);
        $this->assign('title', '市商列表');
        return $this->fetch();
    }

    /**
     * 详情
     * @return mixed
     */
    public function one()
    {
        $uid = input('uid');
        $id = input('id');

        if (!$uid && !$id) {
            $this->error('非法操作！');
        }

        $rowInfo = [];

        if ($uid > 0) {
            $rowInfo = tradersModel::where('uid', $uid)->find();
        }

        if ($id > 0) {
            $rowInfo = tradersModel::where('id', $id)->find();
        }

        $this->assign('uid', $uid ? $uid : $rowInfo->uid);
        $this->assign('row_info', $rowInfo);
        $this->assign('title', '详情');
        return $this->fetch();
    }

    /**
     * 添加编辑市商
     */
    public function addOredit()
    {
        $data = request()->post();
        $id = $data['id'];

        if (!$data['realname'] || !$data['bank_number'] || !$data['alipay_account'] || !$data['wechat_account']) {
            $this->error('真实姓名，银行卡，支付宝，微信信息必填');
        }

        if ($id) {
            $rowInfo = tradersModel::where('bank_number', $data['bank_number'])
                ->whereOr('alipay_account', $data['alipay_account'])
                ->whereOr('wechat_account', $data['wechat_account'])
                ->find();

            if ($rowInfo->id != $id) {
                $this->apiError('该商户已存在！');
            }
            unset($data['id']);
            $data['update_time'] = time();
            $res = tradersModel::where('id', $id)->update($data);
        } else {
            $rowInfo = tradersModel::where('bank_number', $data['bank_number'])
                ->whereOr('alipay_account', $data['alipay_account'])
                ->whereOr('wechat_account', $data['wechat_account'])
                ->find();
            if ($rowInfo) {
                $this->apiError('该商户已存在！');
            }
            $data['create_time'] = time();
            $data['update_time'] = time();
            $res = tradersModel::insert($data);
        }

        if ($res) {
            $this->success('操作成功！', url('lists'));
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        $ids = input('post.id');
        if (!is_array($ids)) {
            $ids = (array)$ids;
        }

        $res = tradersModel::where('id', 'in', $ids)->update(['status' => -2]);
        if ($res) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    /**
     * 市商充值扣币
     * @return mixed
     */
    public function handleMoney()
    {
        if (request()->isPost()) {

            $code = input('code');
            if ($code != session('email_code')) {
                $this->error('邮箱验证码错误');
            }

            $money = input('money', 0);
            if ($money <= 0) {
                $this->error('金额不能小于0');
            }

            $id = input('id');
            $tInfo = \app\common\model\Traders::find($id);
            if (!$tInfo->id) {
                $this->error('该市商不存在');
            }

            $moneytype = 3;
            $field1 = 'haswealth';
            $field2 = 'allwealth';

            $type = input('type');
            if ($type == 'inc') {
                $recordtype = 1;
                $changetype = 35;
                $tInfo->hasmoney += $money;
                $tInfo->totalmoney += $money;
            } else {
                $recordtype = 0;
                $changetype = 36;
                if ($tInfo->hasmoney < $money) {
                    $this->error('账户余额不足！');
                }
                $tInfo->hasmoney -= $money;
            }

            $res = $tInfo->save();
            if ($res) {
                $arr = array('recordtype' => $recordtype, 'changetype' => $changetype, 'moneytype' => $moneytype);
                $money_arr = array('money' => $money, 'hasmoney' => $tInfo->hasmoney);
                money_change($arr, $tInfo->uid, 0, $money_arr, '', 'trader');

                session('email_code', null);
                $this->success('操作成功', url('TradingHall/bonusChange?changetype=' . $changetype));
            } else {
                $this->error('操作失败');
            }
        } else {
            if (!self::$adminInfo['email']) {
                $this->error('缺少邮箱，不可充值扣币');
            }

            $id = input('id');
            $tInfo = \app\common\model\Traders::find($id);
            if (!$tInfo->id) {
                $this->error('非法操作！');
            }

            $this->assign('email', self::$adminInfo['email']);

            $this->assign('tInfo', $tInfo);
            $this->assign('title', '充币扣币');
            return $this->fetch('handlemoney');
        }
    }
}